Refactor CopilotClient initialization and enhance documentation#519
Refactor CopilotClient initialization and enhance documentation#519brettcannon wants to merge 12 commits intogithub:mainfrom
Conversation
… arguments Replace the options dict parameter with explicit keyword-only arguments for better IDE support, type checking, and discoverability. Update docstrings, examples, and all call sites accordingly.
Update cli_path and cwd parameters to accept Union[str, os.PathLike[str], None]. Values are normalized to str via os.fspath() before storage.
- Fix stale event['type'] to event.type.value in API Reference example - Update cli_path/cwd types from (str) to (str | PathLike) - Fix cli_path default description: remove stale COPILOT_CLI_PATH reference - Document log_level valid values (none, error, warning, info, debug, all) - Export LogLevel type from copilot/__init__.py
…ecking Add two @overload declarations to distinguish between cli_url mode (connecting to an external server) and cli_path mode (launching a local CLI process). This provides clearer IDE autocompletion and type-checker support for mutually exclusive parameter groups. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add env parameter to CopilotClient Options and update type annotations to show | None for all parameters that accept None. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
# Conflicts: # python/copilot/client.py
There was a problem hiding this comment.
Pull request overview
Refactors the Python SDK’s CopilotClient initialization to use keyword-only parameters (with overloads for better type checking), adds PathLike support for cli_path/cwd, and updates tests and Python README examples to match the new API.
Changes:
- Reworked
CopilotClient.__init__to accept keyword arguments (and addedtyping.overloadsignatures) plusos.fspath()normalization for path-like inputs. - Updated unit + E2E tests to call
CopilotClient(...)with keyword args; added unit tests covering PathLike inputs. - Updated
python/README.mdto document new parameters/types (including valid log levels andenv) and adjusted example usage.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| python/test_client.py | Updates constructor usage and adds PathLike argument tests. |
| python/e2e/testharness/context.py | Updates E2E harness to instantiate CopilotClient via keyword args. |
| python/e2e/test_session.py | Updates session resume E2E to use keyword args for CopilotClient. |
| python/e2e/test_client.py | Updates E2E client tests to use keyword args. |
| python/copilot/client.py | Implements keyword-only constructor, adds overloads, supports PathLike inputs, updates docstrings/examples. |
| python/copilot/init.py | Exports LogLevel in the public Python package surface. |
| python/README.md | Updates Python README examples and documents updated CopilotClient parameters and log levels. |
Comments suppressed due to low confidence (2)
python/copilot/client.py:246
envis only copied intoself.optionswhen it is truthy (if env:). This means callers cannot intentionally pass an empty dict to fully clear the subprocess environment, and it also makes the README semantics (“replaces the inherited process environment”) unreliable. Checkenv is not Noneinstead of truthiness when deciding whether to persist the provided env.
if cli_url:
self.options["cli_url"] = cli_url
if env:
self.options["env"] = env
python/copilot/client.py:148
- This change removes the previous
CopilotClient(options: dict)calling pattern entirely (constructor is now keyword-only). Since this is the public entry point for the Python SDK, consider keeping backwards compatibility by accepting the oldoptionsdict (and mapping it to keyword args) with a deprecation warning, or explicitly documenting the breaking change and bumping the package version accordingly.
def __init__(
self,
*,
cli_path: Union[str, os.PathLike[str], None] = None,
cli_url: Optional[str] = None,
cwd: Union[str, os.PathLike[str], None] = None,
port: int = 0,
use_stdio: Optional[bool] = None,
log_level: LogLevel = "info",
auto_start: bool = True,
auto_restart: bool = True,
github_token: Optional[str] = None,
use_logged_in_user: Optional[bool] = None,
env: Optional[dict[str, str]] = None,
):
| github_token: Optional[str] = None, | ||
| use_logged_in_user: Optional[bool] = None, | ||
| env: Optional[dict[str, str]] = None, | ||
| ): |
There was a problem hiding this comment.
Managing all these overloads feels painful but if this is what you recommend as the most idiomatic Python solution then I'll gladly defer to you.
Do IDEs really not provide good hinting when passing an object literal in as an argument?
There was a problem hiding this comment.
I don't quite follow what you mean by "good hinting when passing an object literal"? The overloads are there for type checkers because you have arguments that are valid only in certain situations (e.g. cli_args only applies when cli_path is provided).
| Mutually exclusive with cli_path and use_stdio. | ||
| cwd: Working directory for the CLI process. Accepts strings | ||
| or path-like objects (default: current working directory). | ||
| port: Port for the CLI server in TCP mode (default: 0 for random). |
There was a problem hiding this comment.
| port: Port for the CLI server in TCP mode (default: 0 for random). | |
| port: Port for the CLI server in TCP mode (default: 0 for OS-assigned). |
SteveSandersonMS
left a comment
There was a problem hiding this comment.
Looks good, though I think there are also a lot of samples to update under https://github.com/github/copilot-sdk/blob/main/test/scenarios
Refactor the
CopilotClientconstructor to use keyword arguments for improved IDE support and type checking. Update the constructor to accept path-like objects and document changes in the README, including valid log levels and new parameters. Add overload signatures for better type checking and clarify nullable types in the documentation.